package com.yubest.demo.controller;

import com.yubest.demo.dto.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.*;

import javax.mail.internet.MimeMessage;

/**
 * @Author hweiyu
 * @Description
 * @Date 2021/3/1 14:01
 */
@RestController
@Slf4j
public class DemoController {

    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String fromUser;

    @GetMapping(value = "/send")
    public Response send() {
        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            //发件人
            helper.setFrom(fromUser);
            //收件人
            helper.setTo("xxxxxx@qq.com");
            //标题
            helper.setSubject("Hello");
            //文本
            helper.setText("你好！");
            javaMailSender.send(mimeMessage);
        } catch (Exception e) {
            log.error("", e);
        }
        return Response.success();
    }

}
